//
//  JYShopCarViewModel.m
//  JYFarm
//
//  Created by 花花 on 2017/10/21.
//  Copyright © 2017年 花花. All rights reserved.
//

#import "JYShopCartViewModel.h"
#import "JYShopModel.h"

@interface JYShopCartViewModel()

@property (nonatomic, strong) NSMutableArray *shopcartListArray;    /**< 购物车数据源 */

@end
@implementation JYShopCartViewModel

- (void)requestShopcartProductList{
//    NSArray *arr = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"deals.plist" ofType:nil]];
//    
//    NSMutableArray *dataArr = [NSMutableArray arrayWithArray:arr];
//    
//    self.shopcartListArray = [JYShopModel mj_objectArrayWithKeyValuesArray:dataArr];
//    
//    [self.delegate shopcartRequestDataSuccessWithArray:self.shopcartListArray];
//
//    
    [HTTPRequest POST:kUserShoppingTrolleyListUrl parameter:@{@"userId":@"cfdd103c-f7ac-495d-b92b-eeb67a1d1e3b"} success:^(id resposeObject) {
        
        
        
        
        if (SUCCESS) {
            
            
            self.shopcartListArray = [JYShopModel mj_objectArrayWithKeyValuesArray:resposeObject[@"bean"]];
            
            [self.delegate shopcartRequestDataSuccessWithArray:self.shopcartListArray];
        }else{
            
            [MBProgressHUD LY_ShowError:resposeObject[@"msg"] time:2.0];
        }
    } failure:^(NSError *error) {
        
        
        [MBProgressHUD LY_ShowError:(kNoNetworkTips) time:2.0];
    }];


}

- (void)selectProductAtIndexPath:(NSIndexPath *)indexPath isSelected:(BOOL)isSelected{
    
    JYShopModel *model = [self.shopcartListArray safeObjectAtIndex:indexPath.row];
    
    model.selected = isSelected;
    
    [self.delegate shopcartAccountForTotalPrice:[self accountTotalPrice] totalCount:[self accountTotalCount] isAllSelected:[self isAllSelectd]];
    
}

- (void)changeCountAtIndexPath:(NSIndexPath *)indexPath count:(NSInteger)count{
    
    JYShopModel *productModel =[self.shopcartListArray safeObjectAtIndex:indexPath.row];
    
    if (count <= 0) {
        count = 1;
    } else if (count > 10000) {
        count = productModel.goodsAmount;
    }
    
    //根据请求结果决定是否改变数据
    productModel.goodsAmount = count;
    
    [self.delegate shopcartAccountForTotalPrice:[self accountTotalPrice] totalCount:[self accountTotalCount] isAllSelected:[self isAllSelectd]];
    
}
//是否全部选择
- (void)selectAllProductWithStatus:(BOOL)isSelected{
    
    
    for (JYShopModel *model in self.shopcartListArray) {
        model.selected = isSelected;
    }
    [self.delegate shopcartAccountForTotalPrice:[self accountTotalPrice] totalCount:[self accountTotalCount] isAllSelected:[self isAllSelectd]];
    
}
- (void)deleteProductAtIndexPath:(NSIndexPath *)indexPath{
    
    
    JYShopModel *model =  [self.shopcartListArray  safeObjectAtIndex:indexPath.row];
    
    [self.shopcartListArray removeObject:model];
    
    
    if (self.shopcartListArray.count == 0) {
        
        [self.shopcartListArray removeObject:model];
    }else{
        
        for (JYShopModel *model in self.shopcartListArray) {
            
            if (!model.selected) {
                break;
            }
        }
        
    }
    [self.delegate shopcartAccountForTotalPrice:[self accountTotalPrice] totalCount:[self accountTotalCount] isAllSelected:[self isAllSelectd]];
    
    
    if (self.shopcartListArray.count == 0) {
        [self.delegate shopcartClearDeleteAllProducts];
    }
    
}
- (void)beginToDeleteSelectedProducts{
    
    
    NSMutableArray *selectedArr = [NSMutableArray array];
    
    for (JYShopModel *model in self.shopcartListArray) {
        
        if (model.selected) {
            
            [selectedArr addObject:model];
        }
    }
    
    [self.delegate shopcartWillDeleteSelectedProducts:selectedArr];
    
    
}

//将选中的商品进行删除
- (void)deleteSelectedProducts:(NSArray *)selectedArray{
    
    //    //网络请求
    //    //根据请求结果决定是否批量删除
    
    [self.shopcartListArray removeObjectsInArray:selectedArray];
    
    [self.delegate shopcartAccountForTotalPrice:[self accountTotalPrice] totalCount:[self accountTotalCount] isAllSelected:[self isAllSelectd]];
    
    
    if (self.shopcartListArray.count == 0) {
        [self.delegate shopcartClearDeleteAllProducts];
    }
}
//全部删除
- (void)deleteAllProducts{
    //
    //    for (JYShopModel *model in self.shopcartListArray) {
    //
    //        [self.shopcartListArray removeObject:model];
    //    }
    
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    
    params[@"userId"] = @"cfdd103c-f7ac-495d-b92b-eeb67a1d1e3b";
    params[@"deletShoppingTrolleyType"] = @"1";
    
    [HTTPRequest POST:kDeleteuserShoppingTrolleyUrl parameter:params success:^(id resposeObject) {
    
        if (SUCCESS) {
            
            [self.shopcartListArray removeAllObjects];
            
            [self.delegate shopcartClearDeleteAllProducts];
        
    }else{
        
        [MBProgressHUD LY_ShowError:resposeObject[@"msg"] time:2.0];
    }
     } failure:^(NSError *error) {
         
         
         [MBProgressHUD LY_ShowError:(kNoNetworkTips) time:2.0];
     }];

    
}


//提交订单
- (void)commitOdrerSelectedProducts{
    
    NSMutableArray *commitOrderArr = [NSMutableArray array];
    for (JYShopModel *model in self.shopcartListArray) {
        
        if (model.selected) {
            
            [commitOrderArr addObject:model];
        }
        
    }
    
    if (commitOrderArr.count) {}
    
    [self.delegate shopcartCommitOrderForSelectedProducts:commitOrderArr];
    
    
}

- (double)accountTotalPrice{
    
    double totalPrice = 0.0f;
    
    for (JYShopModel *model in self.shopcartListArray) {
        
        if (model.selected) {
            
            totalPrice += [model.goodsPrice doubleValue]* model.goodsAmount;
            
            
        }
    }
    
    return totalPrice;
    
}


- (NSInteger) accountTotalCount{
    
    return 10;
    
    
    //    NSInteger totalCount = 0;
    //
    //    for (JVShopcartBrandModel *brandModel in self.shopcartListArray) {
    //        for (JVShopcartProductModel *productModel in brandModel.products) {
    //            if (productModel.isSelected) {
    //                totalCount += productModel.productQty;
    //            }
    //        }
    //    }
    //
    //    return totalCount;
}


- (BOOL)isAllSelectd{
    
    if (self.shopcartListArray.count == 0)return NO;
    
    BOOL isAllSelected = YES;
    
    for (JYShopModel *model in self.shopcartListArray) {
        if (model.selected == NO) {
            
            isAllSelected = NO;
        }
    }
    return isAllSelected;
    
}
- (void)starProductAtIndexPath:(NSIndexPath *)indexPath{
    
    
    NSLog(@"收藏%zd",indexPath);
}

@end
